package com.guofen.service.hsmpy.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.HsmMiddleWormMapper;
import com.guofen.dao.HsmSmallWormMapper;
import com.guofen.pojo.HsmMiddleWorm;
import com.guofen.pojo.HsmSmallWorm;
import com.guofen.service.hsmpy.MiddleWormBreedingFeedService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MiddleWormBreedingFeedServiceImpl implements MiddleWormBreedingFeedService {

    @Autowired
    private HsmMiddleWormMapper hsmMiddleWormMapper;
    @Autowired
    private HsmSmallWormMapper hsmSmallWormMapper;

    @Override
    public List<Map<String, HsmSmallWorm>> getlist_fh() {
        return hsmMiddleWormMapper.getlist_fh();
    }

    @Override
    public List<Map<String, HsmSmallWorm>> getlist_first() {
        return hsmMiddleWormMapper.getlist_first();
    }

    @Override
    public List<Map<String, HsmSmallWorm>> getlist_second() {
        return hsmMiddleWormMapper.getlist_second();
    }

    @Override
    public boolean add(Integer id, String batch, String worm_group, String small_bug_breed_box_number, String big_bug_breed_start_time, Double small_bug_box_material_weight, Double small_bug_material_weight,Integer number, Double per_bug_material_weight,
                       Double big_bug_per_box_breed_material_weight, Integer serial, String go_where, String big_bug_breed_box_number, Double big_bug_number, Double big_bug_material_target_value, Double big_bug_material_get_value, String big_bug_breed_end_time) {

        String date = DateAndTimeUtil.getDate();

        //首先判断是否能继续加料
        Double factSumNumber = hsmMiddleWormMapper.getsum_big_bug_number(batch, worm_group, small_bug_breed_box_number);
        Double num = Double.parseDouble(number.toString());
        if (factSumNumber == null || factSumNumber < num) {
            HsmMiddleWorm hsmMiddleWorm = new HsmMiddleWorm();
            hsmMiddleWorm.setDate(date);
            hsmMiddleWorm.setBigBugBreedStartTime(big_bug_breed_start_time);
            hsmMiddleWorm.setBatch(batch);
            hsmMiddleWorm.setWormGroup(worm_group);
            hsmMiddleWorm.setSmallBugBreedBoxNumber(small_bug_breed_box_number);
            hsmMiddleWorm.setSmallBugBoxMaterialWeight(small_bug_box_material_weight);
            hsmMiddleWorm.setSmallBugMaterialWeight(small_bug_material_weight);
            hsmMiddleWorm.setNumber(number);
            hsmMiddleWorm.setPerBugMaterialWeight(per_bug_material_weight);
            hsmMiddleWorm.setBigBugPerBoxBreedMaterialWeight(big_bug_per_box_breed_material_weight);
            hsmMiddleWorm.setSerial(serial);
            hsmMiddleWorm.setGoWhere(go_where);
            hsmMiddleWorm.setBigBugBreedBoxNumber(big_bug_breed_box_number);
            hsmMiddleWorm.setBigBugNumber(big_bug_number);
            hsmMiddleWorm.setBigBugMaterialTargetValue(big_bug_material_target_value);
            hsmMiddleWorm.setBigBugMaterialGetValue(big_bug_material_get_value);
            hsmMiddleWorm.setBigBugBreedEndTime(big_bug_breed_end_time);

            int result = hsmMiddleWormMapper.insert(hsmMiddleWorm);
            if (id != null && result == 1){
                HsmSmallWorm hsmSmallWorm = hsmSmallWormMapper.selectByPrimaryKey(id);
                hsmSmallWorm.setFlag(true);
                hsmSmallWormMapper.updateByPrimaryKey(hsmSmallWorm);
                return true;
            }
            return true;
        } else {
            return false;
        }
    }



    @Override
    public List<Map<String, HsmMiddleWorm>> getFirstList(Integer id) {
        //根据小虫培育过程对应的主键ID，查找对应的批次、组别、小虫培育盒号
        //返回字段：中虫培育过程中未进行第一次加料的id，date，中虫培育开始日期，批次，组别，小虫盒号，中虫盒号
        HsmSmallWorm hsmSmallWorm = hsmSmallWormMapper.selectByPrimaryKey(id);
        String batch = hsmSmallWorm.getBatch();
        String worm_group = hsmSmallWorm.getWormGroup();
        String small_bug_breed_box_number = hsmSmallWorm.getHatchBoxNumber();

        return hsmMiddleWormMapper.getFirstList(batch, worm_group, small_bug_breed_box_number);
    }

    @Override
    public boolean first_add(Integer id, String big_bug_breed_start_time, String big_bug_breed_box_number, Integer num,
                             Double big_bug_first_breed_material_w, String remark) {
        HsmMiddleWorm hsmMiddleWorm = hsmMiddleWormMapper.selectByPrimaryKey(id);
        hsmMiddleWorm.setBigBugBreedStartTime(big_bug_breed_start_time);
//        hsmMiddleWorm.setBigBugBreedBoxWeight(big_bug_breed_box_w);
        hsmMiddleWorm.setBigBugBreedBoxNumber(big_bug_breed_box_number);
        hsmMiddleWorm.setNumber(num);
        hsmMiddleWorm.setBigBugFirstBreedMaterialWeight(big_bug_first_breed_material_w);
        hsmMiddleWorm.setRemark(remark);
        int result = hsmMiddleWormMapper.updateByPrimaryKey(hsmMiddleWorm);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getFirstLatest() {
        return hsmMiddleWormMapper.getFirstLatest();
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getSecondList(Integer id) {
        //根据小虫培育过程对应的主键ID，查找对应的批次、组别、小虫培育盒号
        //返回字段：中虫培育过程中已完成第一次加料、未第二次加料的id，date，批次，组别，小虫盒号，中虫盒号
        HsmSmallWorm hsmSmallWorm = hsmSmallWormMapper.selectByPrimaryKey(id);
        String batch = hsmSmallWorm.getBatch();
        String worm_group = hsmSmallWorm.getWormGroup();
        String small_bug_breed_box_number = hsmSmallWorm.getHatchBoxNumber();

        return hsmMiddleWormMapper.getSecondList(batch, worm_group, small_bug_breed_box_number);
    }

    @Override
    public boolean second_add(Integer id, String second_feed_date, Double big_bug_second_breed_material_w, String remark) {
        HsmMiddleWorm hsmMiddleWorm = hsmMiddleWormMapper.selectByPrimaryKey(id);
        //中虫培育开始日期
        String big_bug_breed_start_time = hsmMiddleWorm.getBigBugBreedStartTime();
        //中虫培育结束日期 = 中虫培育开始日期 + 2天
        int result = 0;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(big_bug_breed_start_time);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(date);
            rightNow.add(Calendar.DAY_OF_YEAR,2);//日期加2天
            Date dt1=rightNow.getTime();
            String big_bug_breed_end_time = sdf.format(dt1);
            hsmMiddleWorm.setSecondFeedDate(second_feed_date);
            hsmMiddleWorm.setBigBugSecondBreedMaterialWeight(big_bug_second_breed_material_w);
            hsmMiddleWorm.setRemark(remark);
            hsmMiddleWorm.setBigBugBreedEndTime(big_bug_breed_end_time);
            result = hsmMiddleWormMapper.updateByPrimaryKey(hsmMiddleWorm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getSecondLatest() {
        return hsmMiddleWormMapper.getSecondLatest();
    }

    @Override
    public List<Map<String, HsmMiddleWorm>> getlist() {
        return hsmMiddleWormMapper.getlist();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<HsmMiddleWorm> list = hsmMiddleWormMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<HsmMiddleWorm> list = hsmMiddleWormMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return hsmMiddleWormMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(HsmMiddleWorm record) {
        return hsmMiddleWormMapper.updateByPrimaryKeySelective(record);
    }
}
